// pages/equipment/equipment.js
var ConnectDic={}
var uuid=''
var devicesId = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    x:0,
    y:0,
    z:0,
    direction:0,
    scanContent:'',
    clipboardContent:'',
    copyContent:'',
    BLEState:'',
    BLEServiceArr:[],
    ConnectDevices:[],
    BrightnessValue:0.0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this
    wx.onAccelerometerChange(function(res){
      that.setData({
        x:res.x,
        y:res.y,
        z:res.z
      })
    })
    wx.onCompassChange(function(res){
      that.setData({
        direction:res.direction
      })
    })

    this.getScreenBrightness()

    wx.onUserCaptureScreen(function (res) {
      console.log('用户截屏了')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 获取系统信息1
  getSystemInfo1:function(){
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.model)            //手机型号  
        console.log(res.pixelRatio)       //设备像素比
        console.log(res.windowWidth)      //屏宽
        console.log(res.windowHeight)     //屏高
        console.log(res.language)         //微信语言
        console.log(res.version)          //版本号
        console.log(res.platform)         //客户端平台
      },
    })
  },
  // 获取系统信息2
  getSystemInfo2:function(){
    try{
      var res = wx.getSystemInfoSync()
      console.log(res.model)            //手机型号  
      console.log(res.pixelRatio)       //设备像素比
      console.log(res.windowWidth)      //屏宽
      console.log(res.windowHeight)     //屏高
      console.log(res.language)         //微信语言
      console.log(res.version)          //版本号
      console.log(res.platform)         //客户端平台
    }catch(e){

    }
  },
  // 检测api是否可用
  canIUse:function(){
    console.log(wx.canIUse('getSystemInfo.success.screenWidth'))
    console.log(wx.canIUse('getSystemInfoSync.return.screenWidth'))
    console.log(wx.canIUse('request.object.method.GET'))
  },
  // 获取网络状态
  getNetworkType:function(){
    wx.getNetworkType({
      success: function(res) {
        console.log(res.networkType)
      },
    })

    wx.onNetworkStatusChange(function(res){
      console.log(res.isConnected)
      console.log(res.networkType)
    })
  },
  // 开始监听加速度
  startAccelerometer:function(){
    wx.startAccelerometer({
      success:function(res){
        console.log('开始监听了')
      }
    })
  },
  // 停止监听加速度
  stopAccelerometer:function(){
    wx.stopAccelerometer({
      success:function(res){
        console.log('停止监听了')
      }
    })
  },
  // 开始监听罗盘
  startCompass:function(){
    wx.startCompass({
      success(e){
        console.log('开始监听了')
      }
    })
  },
  //停止监听罗盘
  stopCompass:function(){
    wx.stopCompass({
      success(e) {
        console.log('停止监听了')
      }
    })
  },
  // 拨打电话
  makePhoneCall:function(){
    wx.makePhoneCall({
      phoneNumber: '15605888523',
      success(res){
        console.log('拨打成功')
      },
      fail(res){
        console.log('拨打失败',res)
      }
    })
  },
  // 扫描二维码
  scanCode:function(){
    var that = this
    wx.scanCode({
      success:(res)=>{
        console.log('扫描成功了',res)
        that.setData({
          scanContent:res.result
        })
      },
      fail(res){
        console.log('扫描失败了',res)
      }
    })
  },
  // 输入框
  bindChange:function(e){
    this.setData({
      clipboardContent:e.detail.value
    })
  },
  // 复制
  setClipboard:function(){
    wx.setClipboardData({
      data: this.data.clipboardContent,
      success(res){
        console.log('复制成功')
      }
    })
  },
  // 粘贴
  getClipboard:function(){
    var that = this
    wx.getClipboardData({
      success(res){
        that.setData({
          copyContent:res.data
        })
      }
    })
  },
  // 初始化蓝牙
  openbluetooth:function(){
    var that = this
    wx.openBluetoothAdapter({
      success: function(res) {
        console.log('开启成功')
        that.setData({
          BLEState:'蓝牙初始化成功'
        })
      },
      fail:function(res){
        console.log('失败',res)
        that.setData({
          BLEState: res.errMsg
        })
      }
    })
  },
  // 关闭蓝牙
  closebluetooth:function(){
    var that = this
    wx.closeBluetoothAdapter({
      success: function(res) {
        console.log('关闭成功')
        that.setData({
          BLEState: '蓝牙关闭成功'
        })
      },
      fail:function(res){
        console.log('失败', res)
        that.setData({
          BLEState: res.errMsg
        })
      }
    })
  },
  // 获取蓝牙状态
  getBLEState:function(){
    var that = this
    wx.getBluetoothAdapterState({
      success: function(res) {
        console.log('获取成功')
        that.setData({
          BLEState: '是否正在搜索设备:'+res.discovering.toString()+' 蓝牙适配器是否可用:'+res.available.toString()
        })
      },
    })
  },
  // 监听蓝牙状态变化
  onBLTStateChange:function(){
    var that = this
    wx.onBluetoothAdapterStateChange(function(res){
      that.setData({
        BLEState: '是否正在搜索设备:' + res.discovering.toString() + ' 蓝牙适配器是否可用:' + res.available.toString()
      })
    })
  },
  // 开始搜寻附近蓝牙设备
  startBLEDevicesDis:function(){
    wx.startBluetoothDevicesDiscovery({
      success: function(res) {
        console.log('开始搜寻设备')
      },
    })
  },
  // 停止搜寻附近设备
  stopBLEDevicesDic:function(){
    wx.stopBluetoothDevicesDiscovery({
      success: function(res) {
        console.log('停止搜寻设备')
      },
    })
  },
  // 获取所有设备
  getBLEDevices:function(){
    var that = this
    wx.getBluetoothDevices({
      success: function(res) {
        that.setData({
          BLEServiceArr:res.devices
        })
      },
    })
  },
  //根据 uuid 获取处于已连接状态的设备
  getConnectBLEDevices:function(){
    var that = this
    var marr = []
    for(let i=0;i<this.data.BLEServiceArr.length;i++){
      let object = this.data.BLEServiceArr[i]
      marr.push(object.deviceId)
    }
    console.log('uuid',uuid)
    wx.getConnectedBluetoothDevices({
      services: [uuid],
      success: function(res) {
        console.log('成功',res.devices)
        that.setData({
          ConnectDevices: res.devices
        })
      },
      fail:function(res){
        console.log('失败',res.errMsg)
      }
    })
  },
  // 监听寻找到新设备的事件
  onBluetoothDeviceFound:function(){
    var that = this
    wx.onBluetoothDeviceFound(function(res){
      console.log('监听中',res.devices)
      that.setData({
        BLEServiceArr: res.devices
      })
    })
  },
  // 链接蓝牙
  createBLEConnection:function(e){
    var that = this
    let index = e.target.id
    let item = this.data.BLEServiceArr[index]
    let isConnect = ConnectDic[item.deviceId]
    console.log(isConnect)
    if(isConnect !=undefined || isConnect){
      //已连接
      wx.closeBLEConnection({
        deviceId: item.deviceId,
        success: function(res) {
          console.log('断开链接成功')
          ConnectDic[item.deviceId] = 0
        },
        fail: function (res) {
          console.log('断开链接失败', res.errMsg)
        }
      })
    }else{
      //未连接
      wx.createBLEConnection({
        deviceId: item.deviceId,
        success: function (res) {
          console.log('链接成功')
          ConnectDic[item.deviceId] = 1
        },
        fail: function (res) {
          console.log('链接失败', res.errMsg)
        }
      })
    }
    console.log(item.deviceId)
  },
  // 获取蓝牙设备所有 service（服务）
  getBLEDeviceServices:function(){
    for(let i=0;i<this.data.BLEServiceArr.length;i++){
      let item = this.data.BLEServiceArr[i]
      wx.getBLEDeviceServices({
        deviceId: item.deviceId,
        success: function(res) {
          console.log('获取服务成功',res.services)
          uuid = res.services[0].uuid
          devicesId = item.deviceId
        },
        fail:function(res){
          console.log('获取服务失败',res.errMsg)
        }
      })
    }
  },
  // 获取蓝牙设备所有 characteristic（特征值）
  getBLEDeviceCharacteristics:function(){
    wx.getBLEDeviceCharacteristics({
      deviceId: devicesId,
      serviceId: uuid,
      success: function(res) {
        console.log('获取特征成功',res.characteristics)
      },
    })
  },
  // 开始搜索附近的iBeacon设备
  startBeaconDiscovery:function(){
    wx.startBeaconDiscovery({
      uuids: [],
      success:function(){
        console.log('获取成功')
      }
    })
  },
  // 停止搜索附近的iBeacon设备
  stopBeaconDiscovery:function(){
    wx.stopBeaconDiscovery({
      success(res){
        console.log('停止获取')
      }
    })
  },
  // 获取所有已搜索到的iBeacon设备
  getBeacons:function(){
    wx.getBeacons({
      success:function(res){
        console.log('获取所有设备成功',res.beacons)
      }
    })
  },
  // 设置屏幕亮度。
  setScreenBrightness:function(e){
    let value = e.detail.value/100.0
    wx.setScreenBrightness({
      value: value,
      success(res){
        console.log('设置成功')
      }
    })
  },
  // 获取屏幕亮度。
  getScreenBrightness:function(){
    var that = this
    wx.getScreenBrightness({
      success:function(res){
        that.setData({
          BrightnessValue:res.value*100
        })
      }
    })
  },
  // 设置是否保持常亮状态
  setKeepScreenOn:function(e){
    wx.setKeepScreenOn({
      keepScreenOn: e.detail.value,
      success(res){
        console.log('设置成功')
      },
      fail(res){
        console.log('设置失败')
      }
    })
  },
  // 监听截屏时间
  onUserCaptureScreen:function(){
    console.log('111')
    wx.onUserCaptureScreen(function(res){
      console.log('用户截屏了')
    })
  },
  // 较长时间震动
  vibrateLong:function(){
    wx.vibrateLong({
      success(res){
        console.log('震动')
      }
    })
  },
  // 较短时间震动
  vibrateShort:function(){
    wx.vibrateShort({
      success(res) {
        console.log('震动')
      }
    })
  },
  // 添加联系人
  addPhoneContact:function(){
    wx.chooseImage({
      success: function(res) {
        wx.addPhoneContact({
          firstName: '禹菲菲',
          mobilePhoneNumber:'13462461349',
          photoFilePath:res.tempFilePaths[0]
        })
      },
    })
  }
})